//
//  iHDNextStepButton.m
//  iHomeDesign
//
//  Created by WC on 2020/3/22.
//  Copyright © 2021年 YH. All rights reserved.
//

#import "SYYCustomButton.h"


@interface SYYCustomButton ()

@property (nonatomic,copy) void(^ClickBlock)(UIButton *sender);

@end

@implementation SYYCustomButton

- (instancetype)initWithCornerRadius:(CGFloat)cornerRadius withTitle:(NSString *)title font:(CGFloat)font withClickBlock:(void(^)(UIButton *sender))clickBlock{
    if (self = [super init]) {
        
        [self setBackgroundColor:HHColorMain];
        
        if (cornerRadius == 22.5) {
            //切角+阴影
            self.layer.cornerRadius = cornerRadius;       //圆角弧度
            self.layer.shadowOffset =  CGSizeMake(0, 7);  //阴影的偏移量
            self.layer.shadowOpacity = 0.3;               //阴影的不透明度
            self.layer.shadowColor = [HHColorMain CGColor];//阴影的颜色
        }else{
            //切角
            self.layer.cornerRadius = cornerRadius;
            self.layer.masksToBounds = YES;
        }
        
        
        [self.titleLabel setFont:[UIFont systemFontOfSize:font]];
        [self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

        [self setTitle:title forState:UIControlStateNormal];
        
        [self addTarget:self action:@selector(clickButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        
        self.ClickBlock = clickBlock;
        
    }
    return self;
}

- (instancetype)initWithCornerRadius:(CGFloat)cornerRadius withTitle:(NSString *)title font:(CGFloat)font bgColor:(UIColor *)bgColor titleColor:(UIColor *)titleColor withClickBlock:(void(^)(UIButton *sender))clickBlock{
    if (self = [super init]) {
        self.layer.cornerRadius = cornerRadius;
        self.layer.masksToBounds = YES;
        
        self.backgroundColor = bgColor;
        [self setTitleColor:titleColor forState:UIControlStateNormal];

        self.titleLabel.font = [UIFont systemFontOfSize:font];
        [self setTitle:title forState:UIControlStateNormal];
        
        [self addTarget:self action:@selector(clickButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        
        self.ClickBlock = clickBlock;
        
    }
    return self;
}

- (instancetype)initWithBackgroundColor:(UIColor *)backgroundColor cornerRadius:(CGFloat)cornerRadius borderWidth:(CGFloat)borderWidth borderColor:(UIColor *)borderColor title:(NSString *)title titleColor:(UIColor *)titleColor titleFont:(CGFloat)titleFont withClickBlock:(void(^)(UIButton *sender))clickBlock{
    if (self = [super init]) {
        self.backgroundColor = backgroundColor;

        self.layer.cornerRadius = cornerRadius;
        self.layer.masksToBounds = YES;
        
        self.layer.borderWidth = borderWidth;
        self.layer.borderColor = [borderColor CGColor];
        
        [self setTitle:title forState:UIControlStateNormal];

        [self setTitleColor:titleColor forState:UIControlStateNormal];
        self.titleLabel.font = [UIFont systemFontOfSize:titleFont];
        
        [self addTarget:self action:@selector(clickButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        
        self.ClickBlock = clickBlock;

    }
    return self;
}



- (instancetype)initWithGradientBgImgSize:(CGSize)bgImgSize cornerRadius:(CGFloat)cornerRadius clickBlock:(void(^)(UIButton *sender))clickBlock
{
    if (self = [super init]) {
        [self.titleLabel setFont:HHFont16];

        UIImage *btnBgImage = [SYYTools getBGImage:HHColorHex(0x008AFF) and:HHColorHex(0x99A2FF) directionType:1 option:bgImgSize];
        [self setBackgroundImage:[btnBgImage imageByRoundCornerRadius:cornerRadius] forState:UIControlStateNormal];

        //切角
        self.layer.cornerRadius = cornerRadius;
        self.layer.masksToBounds = YES;

        [self addTarget:self action:@selector(clickButtonAction:) forControlEvents:UIControlEventTouchUpInside];

        self.ClickBlock = clickBlock;
    }
    return self;
}
- (instancetype)initWithGradientStartColor:(UIColor*)startColor endColor:(UIColor*)endColor bgImgSize:(CGSize)bgImgSize cornerRadius:(CGFloat)cornerRadius clickBlock:(void(^)(UIButton *sender))clickBlock
{
    if (self = [super init]) {
        UIImage *btnBgImage = [SYYTools getBGImage:startColor and:endColor directionType:1 option:bgImgSize];
        [self setBackgroundImage:[btnBgImage imageByRoundCornerRadius:cornerRadius] forState:UIControlStateNormal];

        //切角
        self.layer.cornerRadius = cornerRadius;
        self.layer.masksToBounds = YES;

        [self addTarget:self action:@selector(clickButtonAction:) forControlEvents:UIControlEventTouchUpInside];

        self.ClickBlock = clickBlock;
    }
    return self;
}
- (void)setStartColor:(UIColor*)startColor endColor:(UIColor*)endColor bgImgSize:(CGSize)bgImgSize cornerRadius:(CGFloat)cornerRadius {
    UIImage *btnBgImage = [SYYTools getBGImage:startColor and:endColor directionType:1 option:bgImgSize];
    [self setBackgroundImage:[btnBgImage imageByRoundCornerRadius:cornerRadius] forState:UIControlStateNormal];
}


- (void)clickButtonAction:(UIButton *)sender{
    if (self.ClickBlock) {
        self.ClickBlock(sender);
        
        sender.enabled = NO;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            sender.enabled = YES;
        });
    }
}



@end

